package com.xiaoxie.service.impl;

import com.xiaoxie.DTO.PageDTO;
import com.xiaoxie.DTO.modifyWordDTO;
import com.xiaoxie.DTO.quick_chooseDTO;
import com.xiaoxie.DTO.wordUpdateDTO;
import com.xiaoxie.entity.User;
import com.xiaoxie.entity.Word;
import com.xiaoxie.mapper.LexiconDao;
import com.xiaoxie.mapper.UserDao;
import com.xiaoxie.mapper.WordDao;
import com.xiaoxie.mapper.learnLogDao;
import com.xiaoxie.service.IWord_tbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小谢
 * @since 2023-10-13
 */
@Service
public class Word_tbServiceImpl implements IWord_tbService {
    @Autowired
    private WordDao wordDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private learnLogDao logDao;

    @Autowired
    private LexiconDao lexiconDao;

    /**
     * 获取当前需要记忆的单词
     *
     * @return
     */
    @Override
    public List<Word> getNeedMemory(String account) {
        List<Word> needMemory;
        User user = userDao.select(account);
        //用户未完成学习，复习单词
        if (user.getLearnFinish().equals("false"))
            needMemory = wordDao.getNeedMemory(account);
        else
            //用户学习完成复习错词
            needMemory = wordDao.getwrong(account);
        return needMemory;
    }

    /**
     * 添加单词
     *
     * @param wordDTO
     */
    @Override
    public Boolean insert(wordUpdateDTO wordDTO, String account) {
        //添加单词,判断单词是否存在
        if (wordDao.getBy(wordDTO, account).isEmpty()) {
            wordDao.insert(wordDTO, account);
            userDao.updateFalseFinish(account);
            logDao.udInsert(userDao.select(account).getId());
            lexiconDao.inset(account,wordDTO.getLexiconID());
            return true;
        }
        return false;
    }

    /**
     * 更新单词数据
     *
     * @param wordDTO
     */
    @Override
    public boolean update(wordUpdateDTO wordDTO, String account) {
        //判断请求数据是否为空
        if (wordDTO.getWord().isEmpty() || wordDTO.getMeaning().isEmpty())
            return false;

        User user = userDao.select(account);
        //用户未完成学习，正常更新单词数据

        Word word = wordDao.getByword(wordDTO, account);
        if (word.getLearn_today()==0){
            if (wordDTO.getAction().equals("right")){
                logDao.udright(userDao.select(account).getId());
            }else
                logDao.udwrong(userDao.select(account).getId());
        }
        logDao.upsum(userDao.select(account).getId());
        //用户未完成学习，正常更新单词数据
        if (user.getLearnFinish().equals("false")) {
            if (wordDTO.getAction().equals("right")) {
                wordDao.right(wordDTO, account);
            } else {
                wordDao.wrong(wordDTO, account);
            }
        } else {
            //用户完成学习，更新今日单词对错信息
            if (wordDTO.getAction().equals("right")) {
                wordDao.rightFinish(wordDTO, account);
            } else {
                wordDao.wrongFinish(wordDTO, account);
            }
        }
        return true;
    }

    /**
     * 分页查询
     *
     * @param pageDTO
     * @param currentAccount
     * @return
     */
    @Override
    public List<Word> pageList(PageDTO pageDTO, String currentAccount) {
        int page = pageDTO.getPage();
        pageDTO.setPage((page - 1) * pageDTO.getTotal());
        return wordDao.pageList(pageDTO, currentAccount);
    }

    /**
     * 获取所有单词
     *
     * @param account
     * @return
     */
    @Override
    public List<Word> getWordList(String account) {
        return wordDao.getAll(account);
    }

    /**
     * 修改单词
     *
     * @param modifyWord
     */
    @Override
    public void wordModify(modifyWordDTO modifyWord) {
        wordDao.modifyWord(modifyWord);
    }

    /**
     * 删除单词
     *
     * @param wordId
     */
    @Override
    public void deleteWord(Integer wordId) {
        Word word = wordDao.getById(wordId);
        wordDao.delete(wordId);
        lexiconDao.inset(word.getUser(),word.getLexicon_id());
    }

    /**
     * 速刷单词业务
     *
     * @param account
     * @return
     */

    @Override
    public List<quick_chooseDTO> quick_choose(String account) {
        List<quick_chooseDTO> quickChooseDTOS = null; // 获取快速选择题目列表
        List<Word> needMemory=null;
        if (userDao.select(account).getLearnFinish().equals("true"))
             needMemory = wordDao.getwrong(account);
        else
            needMemory=wordDao.getNeedMemory(account);
        if (!needMemory.isEmpty()) {
            quickChooseDTOS = new ArrayList<>(needMemory.size());
            for (int i = 0; i < needMemory.size(); i++) {
                quick_chooseDTO quick = new quick_chooseDTO(); // 创建 quick_chooseDTO 对象
                quick.setId(needMemory.get(i).getId());
                quick.setWord(needMemory.get(i).getWord());
                quick.setMeaning(needMemory.get(i).getMeaning());
                quickChooseDTOS.add(quick); // 将 quick_chooseDTO 对象添加到 quickChooseDTOS 列表中
            }
        } else {
            quickChooseDTOS = wordDao.quick_Choose(account);
        }

        List<Word> wordsList = wordDao.getAll(account); // 获取所有单词列表
        int listSize = wordsList.size(); // 单词列表的大小
        Random random = new Random(); // 随机数生成器
        String[] str; // 存储字符串数组，用于设置错误选项
        for (quick_chooseDTO quick : quickChooseDTOS) { // 遍历每个快速选择题目
            Set<String> wrongSet = new HashSet<>(); // 存储错误选项的集合
            str = new String[3];
            while (wrongSet.size() < 3) { // 直到错误选项的数量达到3个
                int index = random.nextInt(listSize); // 随机选择一个索引
                Word selectedWord = wordsList.get(index); // 根据索引获取单词
                if (!selectedWord.getMeaning().equals(quick.getWord()) && !wrongSet.contains(selectedWord.getMeaning())) {
                    // 如果选择的单词不是正确答案且不在错误选项集合中
                    wrongSet.add(selectedWord.getMeaning()); // 将单词添加到错误选项集合中
                }
            }
            quick.setWrong(wrongSet.toArray(str)); // 将错误选项设置到快速选择题目对象中
        }
        return quickChooseDTOS;
    }


    @Override
    public void quickUpdate(Integer id, String action, String account) {
        Word word = wordDao.getById(id);
        User user = userDao.select(account);
        if (action.equals("right")) {//单词记忆正确
            if (user.getLearnFinish().equals("true"))
             if (word.getWrong_today() > 0)//复习错误单词
                wordDao.rightFinish(new wordUpdateDTO(action, word.getWord(), word.getMeaning()), account);
            else {
                wordDao.quickRight(id);
            }else{
                wordDao.right(new wordUpdateDTO(null,word.getWord(),account),account);
            }
        }else{
            if (user.getLearnFinish().equals("true"))
                if (word.getWrong_today() > 0)
                 wordDao.wrongFinish(new wordUpdateDTO(action, word.getWord(), word.getMeaning()), account);
             else{
                wordDao.quickWrong(id);
            }else {
                wordDao.wrong(new wordUpdateDTO(null,word.getWord(),account),account);
            }
        }

        if (word.getLearn_today()==0){
            if (action.equals("right")){
                logDao.udright(userDao.select(account).getId());
            }else
                logDao.udwrong(userDao.select(account).getId());
        }

        logDao.upsum(userDao.select(account).getId());
    }
}
